package com.factory.simplefactory.pizzastore.order;

import com.factory.simplefactory.pizzastore.pizzapojo.Pizza;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @author 薛虎
 * @date 2020/12/17 11:14
 * 简单工厂模式
 */
public class OrderPizzaImprove {

    //定义一个简单工厂对象
    SimpleFactory simpleFactory;

    Pizza pizza = null;

    //构造器
    public OrderPizzaImprove(SimpleFactory simpleFactory) {
        setSimpleFactory(simpleFactory);
    }


    public void setSimpleFactory(SimpleFactory simpleFactory) {
        String orderType = ""; //用户输入的披萨类型
        this.simpleFactory = simpleFactory; //设置一个简单工厂对象

        do {
            //首先获取用户想要订购的披萨类型
            orderType = getType();
            pizza = this.simpleFactory.createPizza(orderType);

            //输出pizza的信息
            if (pizza != null) {  //订购成功
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else {
                System.out.println("订购披萨失败");
                break;
            }
        } while (true);
    }

    //写一个方法,可以获取客户希望订购的披萨种类
    private String getType() {
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input pizza 种类:");
            String str = bufferedReader.readLine();
            return str;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
